/*
 * AlarmUpdate.cpp
 *
 *  Created on: 2015-5-29
 *      Author: root
 */
#include"PublicAlarm.h"
#if USE_PUBLIC_ALARM
#include"PublicMySql.h"
#include"PublicLog.h"
#include"PublicTool.h"
std::string GetAlarmTypeDescr(int alarmtype){
    std::string descr="未知";
    switch(alarmtype){
    case 0:
        descr="CDN服务器";
        break;
    case 1:
        descr="CDN交换机";
        break;
    case 5:
        descr="CDN链路质量";
        break;
    case 6:
        descr="IDC服务器";
        break;
    case 7:
        descr="IDC交换机";
        break;
    case 8:
        descr="IDC链路质量";
        break;
    default:
        descr="未知";
        break;
    }
    return descr;
}
//Id,bjip,bjdk,bjdm,scbjsj,bjcs
int PublicUpdateAlarm(SETALARMINFO alarminfo,const char*SQL,int alarmtype,int mysqltype){
	SETALARMINFO checkinfo;
	SETALARMINFO updateinfo;
	ReSetStartTime();
	MYSQL*mysql=OpenMySQL(mysqltype);
	if(mysql==NULL)return -2;
	my_ulonglong resnum=0;
	MYSQL_RES*res=RunMySQLGetRes(mysql,SQL,resnum);
	if(res!=NULL){
		MYSQL_ROW row=NULL;
		while(res&&((row=MySQLFetchRow(res))!=NULL)){
			ALARMINFO info={0};
			info.ID=atol(row[0]);
			strcpy(info.ip,row[1]);
			if(row[2]!=NULL)strcpy(info.bjdk,row[2]);
			info.bjdm=atol(row[3]);
			info.bjcs=atol(row[5]);
			checkinfo.insert(info);
		}
		FreeMySQLRes(res);
	}else{
		CloseMySQL(mysql);
		WRONG("获取历史报警失败 Type:%s",GetAlarmTypeDescr(alarmtype).c_str());
		return -1;
	}
/////////////////////////////////////////////////////////////////////
	SETALARMINFO::iterator it=alarminfo.begin();
	while(it!=alarminfo.end()){
		ALARMINFO info=*it;
		SETALARMINFO::iterator it2=checkinfo.find(info);
		if(it2!=checkinfo.end()){
			info.ID=it2->ID;
			info.bjcs=it2->bjcs+1;
		}else{
			info.bjcs=1;
		}
		updateinfo.insert(info);
		it++;
	}
/////////////////////////////////////////////////////////////////////
	const char*Head="INSERT INTO `dn_monitor`.`easy_alarmInfo`"
					"(`Id`,`bjip`,`bjdk`,`bjdm`,`jkzt`,`bjz`,`scbjsj`,`bjcs`,`zhbjsj`,`jhj`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE "
                   "`bjcs`=`bjcs`+1,"
                   "`jhj`=values(`jhj`),"
                   "`jkzt`=values(`jkzt`),"
                   "`bjz`=values(`bjz`),"
                   "`bjdk`=values(`bjdk`),"
                   "`zhbjsj`=values(`zhbjsj`);";
	std::string strUpdateSql="";
	int Count=0;
	int InserNum=0;
	int UpdateNum=0;
	int Ret=0;
	it=updateinfo.begin();
	while(it!=updateinfo.end()){
		char UpdateSql[2048]={0};
		if(it->ID==0){
			InserNum++;
			sprintf(UpdateSql,"(NULL,'%s','%s',%d,%d,'%s',unix_timestamp(),%d,unix_timestamp(),%d)",
			it->ip,it->bjdk,it->bjdm,it->jkzt,it->bjz,it->bjcs,alarmtype);
		}else{
			UpdateNum++;
			sprintf(UpdateSql,"(%ld,'%s','%s',%d,%d,'%s',unix_timestamp(),%d,unix_timestamp(),%d)",
			it->ID,it->ip,it->bjdk,it->bjdm,it->jkzt,it->bjz,it->bjcs,alarmtype);
        }
		if(Count==0){
			strUpdateSql=Head;
		}else{
			strUpdateSql+=",";
		}
		strUpdateSql+=UpdateSql;
		Count++;
		if(Count>1000){
			strUpdateSql+=End;
			Ret=RunMySQL(mysql,strUpdateSql.c_str());
			Count=0;
			strUpdateSql="";
			if(Ret!=0)break;
		}
		it++;
	}
	if(Count>0){
		strUpdateSql+=End;
		Ret=RunMySQL(mysql,strUpdateSql.c_str());
		Count=0;
		strUpdateSql="";
	}
	CloseMySQL(mysql);
	INFOEX("GetAlarm:%zd Insert:%d Update:%d Total:%zd Type:%s Ret:%d UseTime:%d ms",
	checkinfo.size(),InserNum,UpdateNum,updateinfo.size(),GetAlarmTypeDescr(alarmtype).c_str(),Ret,GetEndTime());
	return Ret;
}
int PublicUpdateAlarmEx(SETALARMINFO&alarminfo,const char*SQL,int alarmtype,int mysqltype,int bClean){
    if(alarminfo.empty()){
        return 0;
    }
    INFOEX("UpAlarm:%zd Type:%s bClean:%d",alarminfo.size(),GetAlarmTypeDescr(alarmtype).c_str(),bClean);
    int Ret=PublicUpdateAlarm(alarminfo,SQL,alarmtype,mysqltype);
    if(Ret==0||bClean==1){
       alarminfo.clear();
    }
    return Ret;
}
int PublicUpdateAlarmFastBack(SETALARMINFO&alarminfo,const char*SQL,int mysqltype){
	if(alarminfo.empty()){
        return 0;
    }
	SETALARMINFO checkinfo;
	SETIDLIST    idlist;
	int			 Ret=0;
	MYSQL*mysql=OpenMySQL(mysqltype);
	if(mysql==NULL)return -2;
	my_ulonglong resnum=0;
	MYSQL_RES*res=RunMySQLGetRes(mysql,SQL,resnum);
	if(res!=NULL){
		MYSQL_ROW row=NULL;
		while(res&&((row=MySQLFetchRow(res))!=NULL)){
			ALARMINFO info={0};
			info.ID=atol(row[0]);
			strcpy(info.ip,row[1]);
			if(row[2]!=NULL)strcpy(info.bjdk,row[2]);
			info.bjdm=atol(row[3]);
			info.bjcs=atol(row[5]);
			checkinfo.insert(info);
		}
		FreeMySQLRes(res);
	}else{
		CloseMySQL(mysql);
		WRONG("获取历史报警失败 用于快速备份");
		return -1;
	}
	do{
		SETALARMINFO::iterator it=alarminfo.begin();
		while(it!=alarminfo.end()){
			ALARMINFO info=*it;
			SETALARMINFO::iterator it2=checkinfo.find(info);
			if(it2!=checkinfo.end()){
				idlist.insert(it2->ID);
			}
			it++;
		}
	}while(0);
	int Count=0;
	std::string sqlbuff;
	const char*Head="update `dn_monitor`.`easy_alarmInfo` set `lkbf`=1 where `Id` in(";
	SETIDLIST::iterator it=idlist.begin();
	while(it!=idlist.end()){
		char UpdateSql[512]={0};
		if(Count==0){
			sqlbuff=Head;
			sprintf(UpdateSql,"%d",*it);
		}else{
			sprintf(UpdateSql,",%d",*it);
		}
		sqlbuff+=UpdateSql;
		Count++;
		if(Count>500){
			sqlbuff+=")";
			Ret=RunMySQL(mysql,sqlbuff.c_str());
			Count=0;
			if(Ret!=0)break;
		}
		it++;
	}
	if(Count>0){
		sqlbuff+=")";
		Ret=RunMySQL(mysql,sqlbuff.c_str());
		Count=0;	
	}
	CloseMySQL(mysql);
	INFOEX("本次设置快速备份标识 理论:%zd 个 实际:%zd 个",alarminfo.size(),idlist.size());
	return 0;
}
#endif
